package it.koutifaris.travianmap.dal.sqlservices;

import static it.koutifaris.travianmap.dal.sqlservices.Auditable.COLUMN_NAME_REVISION_ID;
import static it.koutifaris.travianmap.dal.sqlservices.MapSqlContract.Alliances.COLUMN_NAME_ALLIANCE_ID;
import static it.koutifaris.travianmap.dal.sqlservices.MapSqlContract.Alliances.COLUMN_NAME_ALLIANCE_NAME;
import static it.koutifaris.travianmap.dal.sqlservices.MapSqlContract.Alliances.TABLE_NAME;
import junit.framework.Assert;
import android.content.ContentValues;
import android.database.SQLException;

public class AlliancesTableTest extends AbstractMapDbTest {

	public void testAlliancesTableExist() {
		getDbHelper().getReadableDatabase().rawQuery("select 1 from " + TABLE_NAME, null);
	}

	public void testUniqueConstraint() {
		ContentValues contentValues = new ContentValues();

		setUniqueKeyData(contentValues);
		setAllianceData(contentValues);

		getDbHelper().getWritableDatabase().insertOrThrow(TABLE_NAME, null, contentValues);
		try {
			changeAlliaceData(contentValues);
			getDbHelper().getWritableDatabase().insertOrThrow(TABLE_NAME, null, contentValues);

			Assert.fail("Unique key on " + COLUMN_NAME_REVISION_ID + " and " + COLUMN_NAME_ALLIANCE_ID + " expected");
		} catch (SQLException e) {
			// OK, test passed
		}

	}

	private void setUniqueKeyData(ContentValues contentValues) {
		contentValues.put(COLUMN_NAME_REVISION_ID, 1);
		contentValues.put(COLUMN_NAME_ALLIANCE_ID, 1);
	}

	private void setAllianceData(ContentValues contentValues) {
		contentValues.put(COLUMN_NAME_ALLIANCE_NAME, "Test Alliance");
	}

	private void changeAlliaceData(ContentValues contentValues) {
		contentValues.put(COLUMN_NAME_ALLIANCE_NAME, "Test Alliance B");
	}

}
